import { default as THREE } from './three.min';

function THREERoot(params) {
    params = utils.extend(
        {
            fov: 60,
            zNear: 10,
            zFar: 100000,

            createCameraControls: true,
        },
        params
    );

    this.renderer = new THREE.WebGLRenderer({
        antialias: params.antialias,
        alpha: true,
    });
    this.renderer.domElement.style.boxSizing = "border-box";
    this.renderer.domElement.style.position = 'absolute';

    this.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio || 1));
    document.getElementById('three-container').appendChild(this.renderer.domElement);

    this.camera = new THREE.PerspectiveCamera(
        params.fov,
        window.innerWidth / window.innerHeight,
        params.zNear,
        params.zfar
    );

    this.scene = new THREE.Scene();

    if (params.createCameraControls) {
        this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
    }

    this.resize = this.resize.bind(this);
    this.tick = this.tick.bind(this);

    this.resize();
    this.tick();

    window.addEventListener('resize', this.resize, false);
}
THREERoot.prototype = {
    tick: function () {
        this.update();
        this.render();
        requestAnimationFrame(this.tick);
    },
    update: function () {
        this.controls && this.controls.update();
    },
    render: function () {
        this.renderer.render(this.scene, this.camera);
    },
    resize: function () {
        let width = window.innerWidth;
        let height = window.innerHeight - 100;
        this.camera.aspect = width / height;
        this.camera.updateProjectionMatrix();

        this.renderer.setSize(width, height);
    },
};

var utils = {
    extend: function (dst, src) {
        for (var key in src) {
            dst[key] = src[key];
        }

        return dst;
    },
    randSign: function () {
        return Math.random() > 0.5 ? 1 : -1;
    },
    ease: function (ease, t, b, c, d) {
        return b + ease.getRatio(t / d) * c;
    },
    fibSpherePoint: (function () {
        var vec = { x: 0, y: 0, z: 0 };
        var G = Math.PI * (3 - Math.sqrt(5));

        return function (i, n, radius) {
            var step = 2.0 / n;
            var r, phi;

            vec.y = i * step - 1 + step * 0.5;
            r = Math.sqrt(1 - vec.y * vec.y);
            phi = i * G;
            vec.x = Math.cos(phi) * r;
            vec.z = Math.sin(phi) * r;

            radius = radius || 1;

            vec.x *= radius;
            vec.y *= radius;
            vec.z *= radius;

            return vec;
        };
    })(),
    spherePoint: (function () {
        return function (u, v) {
            u === undefined && (u = Math.random());
            v === undefined && (v = Math.random());

            var theta = 2 * Math.PI * u;
            var phi = Math.acos(2 * v - 1);

            var vec = {};
            vec.x = Math.sin(phi) * Math.cos(theta);
            vec.y = Math.sin(phi) * Math.sin(theta);
            vec.z = Math.cos(phi);

            return vec;
        };
    })(),
};

export default THREERoot;
